package com.woniuxy.woniuhis.controller;

import com.woniuxy.woniuhis.entity.*;
import com.woniuxy.woniuhis.exception.InpatientServiceException;
import com.woniuxy.woniuhis.mapper.MedicineMapper;
import com.woniuxy.woniuhis.service.MediItemService;
import com.woniuxy.woniuhis.service.RecipeMediService;
import com.woniuxy.woniuhis.util.ResponseInfo;
import com.woniuxy.woniuhis.util.UUIDUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/recipeMedi")
public class RecipeController {
    @Autowired
    private RecipeMediService recipeMediService;

    @Autowired
    private MedicineMapper medicineMapper;

    @Autowired
    private MediItemService mediItemService;

    @RequestMapping("getRecipeMediById")
    @ResponseBody
    public ResponseInfo getRecipeMediById(String recipeMediId) {
        ResponseInfo responseInfo = new ResponseInfo();
        try {
            if (StringUtils.isEmpty(recipeMediId)) {
                return null;
            }
            RecipeMedi recipeMedi = recipeMediService.findRecipeMediById(recipeMediId);
            responseInfo = new ResponseInfo(200, recipeMedi, "ok");
            responseInfo.setState(200);
            responseInfo.setData(recipeMedi);
            return responseInfo;
        } catch (InpatientServiceException e) {
            e.printStackTrace();
            responseInfo.setState(500);
            responseInfo.setMsg(e.getMessage());
            return responseInfo;
        }
    }

    @RequestMapping("addRecipeMedi")
    @ResponseBody
    public ResponseInfo addRecipeMedi(String[] id, String[] amount, String recipeMediId, String sourceId) {
        ResponseInfo responseInfo = new ResponseInfo();
        try {
            if (id != null && amount != null) {
                RecipeMedi recipeMedi = new RecipeMedi();
                recipeMedi.setRecipeMediId(recipeMediId);
                List<MediItem> mediItemList = new ArrayList<>();
                Medicine medicine = null;
                for (int i = 0; i < id.length; i++) {
                    medicine = medicineMapper.selectMediById(id[i]);
                    //封装mediItem数据
                    Double mediItemSubtotal = Integer.parseInt(amount[i]) * medicine.getMediPrice();
                    MediItem mediItem = new MediItem(UUIDUtil.uuid32(), medicine,
                            Integer.parseInt(amount[i]), mediItemSubtotal, recipeMedi, false);
                    mediItemService.addMediItem(mediItem);
                    mediItemList.add(mediItem);
                }
                //封装处方数据
                recipeMedi.setSourceId(sourceId);
                recipeMedi.setRecipeMediDate(new Date());
                //dep暂时先不设置，添加医嘱的时候加入,此时暂时手动模拟一个
                Dep dep = null;
                recipeMedi.setPayStatus(false);
                Double recipeMediTotal = 0.0;
                for (MediItem mediItem : mediItemList) {
                    recipeMediTotal += mediItem.getMediItemSubtotal();
                }
                recipeMedi.setRecipeMediTotal(recipeMediTotal);
                recipeMedi.setMediItemList(mediItemList);
                if (sourceId.startsWith("out")) {
                    dep = new Dep("1", "门诊部");
                    recipeMedi.setDep(dep);
                    recipeMediService.addRecipeMedi(recipeMedi);
                } else {
                    dep = new Dep("1", "住院部");
                    recipeMedi.setDep(dep);
                    recipeMediService.addRecipeMedi(recipeMedi);
                }
            }
            responseInfo.setState(200);
            responseInfo.setData(recipeMediId);
            responseInfo.setMsg("success");
            return responseInfo;
        } catch (Exception e) {
            e.printStackTrace();
            responseInfo.setState(500);
            responseInfo.setMsg(e.getMessage());
            return responseInfo;
        }
    }

}
